﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Engine;
using Gencode;

namespace Library
{
    public class Common
    {
        private static YearInfo _yearInfo = null;
        public static YearInfo yearInfo
        {
            set { _yearInfo = value; }
            get
            {
                if(_yearInfo == null)
                {
                    _yearInfo = new Year().Load();
                }
                return _yearInfo;
            }
        }

        private static ArrayList _classList = null;
        /// <summary>
        /// Danh sách các lớp
        /// </summary>
        public static ArrayList ClassList
        {
            set { _classList = value; }
            get
            {
                if(_classList == null)
                {
                    _classList = (new Class()).Load();
                }
                return _classList;
            }
        }

        private static ArrayList [] _roundList = null;
        /// <summary>
        /// Danh sách các vòng
        /// </summary>
        public static ArrayList [] RoundList
        {
            set { _roundList = value; }
            get
            {
                if(_roundList == null)
                {
                    _roundList = new ArrayList[ClassList.Count];
                    Round round = null;
                    for (int i = 0; i < ClassList.Count; i++)
                    {
                        if(round == null)
                            round = new Round();
                        _roundList[i] = round.Load((ClassInfo)ClassList[i], yearInfo);
                    }
                }
                return _roundList;
            }
        }

        private static Config _config = null;
        public static Config config
        {
            set { _config = value; }
            get
            {
                if(_config == null)
                    _config = Setting.ReadConfig();
                return _config;
            }
        }

        /// <summary>
        /// Log Exception to text file
        /// </summary>
        /// <param name="ex"></param>
        public static void LogException(Exception ex)
        {
            string _fileName = @"log.txt";
            StreamWriter streamWriter = File.AppendText(_fileName);
            streamWriter.WriteLine();
            streamWriter.WriteLine("################################################################################");
            streamWriter.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
            streamWriter.WriteLine(ex.ToString());
            streamWriter.Close();
        }

        public static void LogAction(Action action)
        {
            string _fileName = @"Action.txt";
            StreamWriter streamWriter = File.AppendText(_fileName);
            streamWriter.WriteLine();
            streamWriter.WriteLine("################################################################################");
            streamWriter.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
            streamWriter.WriteLine(User.UserName);
            streamWriter.WriteLine(EnumHelper.GetDescription(action));
            streamWriter.Close();
        }

        private static Dictionary<string, string> _level = null;
        public static Dictionary<string, string> Level
        {
            set { _level = value; }
            get
            {
                if(_level == null)
                {
                    _level = new Dictionary<string, string>();
                }
                if (_level.Count != 3)
                {
                    _level.Clear();
                    _level.Add("0", "Mức dễ");
                    _level.Add("1", "Mức trung bình");
                    _level.Add("2", "Mức khó");
                }
                return _level;
            }
        }
    }
}